package com.project.harmonyledger.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project.harmonyledger.pojo.Transaction;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface TransactionMapper extends BaseMapper<Transaction> {

    @Select("SELECT t.type, SUM(t.amount) as total " +
            "FROM transactions t " +
            "WHERE t.account_id = #{accountId} " +
            "AND t.transaction_date BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY t.type")
    List<Map<String, Object>> getAccountStatistics(@Param("accountId") Long accountId,
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate);

    @Select("SELECT a.name as accountName, t.type, SUM(t.amount) as total " +
            "FROM transactions t " +
            "JOIN accounts a ON t.account_id = a.id " +
            "WHERE t.user_id = #{userId} " +
            "AND t.transaction_date BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY a.name, t.type")
    List<Map<String, Object>> getAllAccountsStatistics(@Param("userId") String userId,
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate);
}